<?php
namespace app\policy\admin;

use app\admin\controller\Admin;
use app\common\builder\ZBuilder;
use think\Db;

class Article extends Admin
{
    public function index(){
        $map = $this->getMap();
        $data_list =Db::name('policy_article')
        ->order('create_time','desc')
        ->where($map)
        ->paginate();
        return ZBuilder::make('table')
        ->hideCheckbox()
        ->addColumns([
            ['id', '编号'],
            ['title', '标题'],
            ['date', '日期'],
            ['create_time', '创建时间'],
        ])
        ->setColumnWidth([ 'id' => 40 ])
        ->addColumn('right_button', '操作', 'btn')
        ->addRightButton('edit',['href' => url('edit', ['id' => '__id__'])], false)
        ->addRightButton('delete',['href' => url('delete', ['id' => '__id__'])])
        ->addTopButton('add', ['title'=>'新增政策',], false)
        ->setRowList($data_list)
        ->fetch();
    }
    public function add(){
        return ZBuilder::make('form')
        ->setPageTitle('添加政策')
        ->addText('title', '标题')
        ->addDate('date', '日期')
        ->addUeditor('content', '内容')
        ->setUrl(url('save'))
        ->fetch();
    }
    public function save()
    {
        if(request()->isPost()){
            $post = request()->post();
            $post['create_time']= date("Y-m-d H:i:s",time());
            Db::name('policy_article')->insert($post)?$this->success('新增成功', url('index'), '_parent_reload'):$this->error('新增失败');
        }
    }
    public function edit($id="")
    {
        if(empty($id)){
            return ZBuilder::make('form')->assign('empty_tips', '请指定修改对象')->hideBtn(['submit', 'back'])->fetch();
        }
        $data = Db::name('policy_article')->find($id);
        // $data['content']=html_entity_decode($data['content']);
        return ZBuilder::make('form')
        
        ->addText('title', '标题')
        ->addDate('date', '日期')
        ->addUeditor('content', '内容')
        ->setFormData($data)
        ->setUrl(url('editSave', ['id' =>$id]))
        ->fetch();
    }

    public function editSave($id){
        $post = request()->post();
        $post['id']=$id;
        Db::name('policy_article')->update($post)?$this->success('修改成功', url('index'), '_parent_reload'):$this->error('修改失败');
    }
    public function delete($id='')
    {
        Db::name('policy_article')->delete($id)?$this->success('删除成功', null, '_parent_reload'):$this->error('删除失败');
    }
}
